'use strict';

const { Controller } = require('egg');

class CaseController extends Controller {
  // 更新病例 --主诉
  async updataCase () {
    const { ctx } = this;
    const { data } = ctx.request.body;
    console.log(data, "updata,data");
    const _ctx = this.ctx;
    const Case = await _ctx.model.Case.update({
      temperature_case: data.temperature_case,
      marriage_case: data.marriage_case, fertility_case: data.fertility_case, meal_case: data.meal_case, previous_history: data.previous_history, main_suit: data.main_suit
    }, {
      where: {
        id_case: data.id_case
      }
    })
    const Patient = await _ctx.model.Patient.update({
      height_patient: data.height_patient, weight_patient: data.weight_patient
    }, {
      where: {
        id_patient: data.id_patient
      }
    })
    ctx.body = { code: 0, data: { case: Case, patient: Patient } }
  }

  // 更新诊断
  async updataCaseDiagnostic () {
    const { ctx } = this;
    const { data } = ctx.request.body;
    console.log(data, "updata,data");
    const _ctx = this.ctx;
    const Case = await _ctx.model.Case.update({
      prescribing_diagnostic: JSON.stringify(data.prescribing_diagnostic),
      physician_order_diagnostic: data.physician_order_diagnostic
    }, {
      where: {
        id_case: data.id_case
      }
    })
    // 更新挂号状态
    const Registration = await _ctx.model.Registration.update({
      state_registration: 1,
    }, {
      where: {
        id_case: data.id_case
      }
    })

    ctx.body = { code: 0, data: { case: Case, registration: Registration } }
  }

  // 更新病例回访
  async updataCaseVisit () {
    const { ctx } = this;
    const { data } = ctx.request.body;
    console.log(data, "updata,data");
    const _ctx = this.ctx;
    const Case = await _ctx.model.Case.update({
      illness_callback: data.illness_callback,
      medication_callback: data.medication_callback,
      direction_medication_callback: data.direction_medication_callback,
      direction_live_callback: data.direction_live_callback,
    }, {
      where: {
        id_case: data.id_case
      }
    })
    ctx.body = { code: 0, data: Case }
  }
}

module.exports = CaseController;
